<?php

class DataMapper
{

  /**
   *
   * @param IDomObjDefinition $definition
   * @param array $data
   * @return array
   */
  public static function inflateCollection(IDomObjDefinition $definition, &$data, $depth = -1)
  {
    if (! empty($data)) {
      foreach ($data as &$row) {
        /*$row = */DataMapper::inflateObj($definition, $row, $depth - 1);
      }
    }

    //return (array) $data;
  }

  /**
   *
   * @param IDomObjDefinition $definition
   * @param array $data
   * @return array
   */
  public static function inflateObj(IDomObjDefinition $definition, &$data, $depth = -1)
  {
    $def = Domain::toEntity($definition);

    /*$name = $def->getClass();
    $id = $data[$def->getPkName()];

    if (DomainRegistry::has($name, $id)) {
      return DomainRegistry::get($name, $id);
    }*/

    //$obj = $definition->factory();


    try {
      $def = Domain::toEntity($definition);

      foreach ($definition->getFields() as $name => $field) {
        $field = $def->getField($name);

        if (isset($data[$field->getName()])) {
          //$obj->$name = $data[$field->getName()];
          DataMapper::inflateValue($data[$field->getName()], $field);

        //unset($data[$field->getName()]);
        }
      }
    }
    catch (DomainTypeConversionException $e) {}

    if ($depth != 0) {
      try {
        $def = Domain::toAggregate($definition);

        foreach ($def->getAssociations() as $name => $assoc) {
          $assoc = $def->getAssociation($name);

          if (isset($data[$name])) {
            if ($assoc->isSingle()) {
              /*$obj->$name = DomainMapper::inflateObj($assoc->getTarget(), $data[$name], $depth - 1);*/
              $data[$name] = DataMapper::inflateObj($assoc->getTarget(), $data[$name], $depth - 1);
            }
            else {
              /*$obj->$name = DomainMapper::inflateCollection($assoc->getTarget(), $data[$name], $depth - 1);*/
              $data[$name] = DataMapper::inflateCollection($assoc->getTarget(), $data[$name], $depth - 1);
            }

          //unset($data[$name]);
          }
        }
      }
      catch (DomainTypeConversionException $e) {}
    }

    //$obj->copyAll($data);
    //$obj->commit();


    //DomainRegistry::set($obj);

    //return $obj;
  }

  public static function inflateValue(&$value, IFieldDefinition $field)
  {
    switch ($field->getType()) {
      case 'int' :
      case 'mediumint' :
      case 'longint' :
      case 'shortint' :
        $value = ( int ) $value;
        break;

      case 'varchar' :
      case 'text' :
        $value = ( string ) $value;
        break;
    }
  }

}

?>